﻿ IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Game_Team]') AND parent_object_id = OBJECT_ID(N'[dbo].[Game]'))
ALTER TABLE [dbo].[Game]  WITH CHECK ADD  CONSTRAINT [FK_Game_Team] FOREIGN KEY([TeamID])
REFERENCES [dbo].[Team] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Entity_EntityType]') AND parent_object_id = OBJECT_ID(N'[dbo].[Entity]'))
ALTER TABLE [dbo].[Entity]  WITH CHECK ADD  CONSTRAINT [FK_Entity_EntityType] FOREIGN KEY([EntityTypeID])
REFERENCES [dbo].[EntityType] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Entity_Team]') AND parent_object_id = OBJECT_ID(N'[dbo].[Entity]'))
ALTER TABLE [dbo].[Entity]  WITH CHECK ADD  CONSTRAINT [FK_Entity_Team] FOREIGN KEY([TeamID])
REFERENCES [dbo].[Team] ([ID])
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_GameEntity_Entity]') AND parent_object_id = OBJECT_ID(N'[dbo].[GameEntity]'))
ALTER TABLE [dbo].[GameEntity]  WITH CHECK ADD  CONSTRAINT [FK_GameEntity_Entity] FOREIGN KEY([EntityID])
REFERENCES [dbo].[Entity] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_GameEntity_Game]') AND parent_object_id = OBJECT_ID(N'[dbo].[GameEntity]'))
ALTER TABLE [dbo].[GameEntity]  WITH CHECK ADD  CONSTRAINT [FK_GameEntity_Game] FOREIGN KEY([GameID])
REFERENCES [dbo].[Game] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_GameEntity_GameEntity]') AND parent_object_id = OBJECT_ID(N'[dbo].[GameEntity]'))
ALTER TABLE [dbo].[GameEntity]  WITH CHECK ADD  CONSTRAINT [FK_GameEntity_GameEntity] FOREIGN KEY([GameID],[ParentGameEntityID])
REFERENCES [dbo].[GameEntity] ([GameID],[EntityID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_GameEntity_GameRole]') AND parent_object_id = OBJECT_ID(N'[dbo].[GameEntity]'))
ALTER TABLE [dbo].[GameEntity]  WITH CHECK ADD  CONSTRAINT [FK_GameEntity_GameRole] FOREIGN KEY([GameRoleID])
REFERENCES [dbo].[GameRole] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EntityAttribute_Entity]') AND parent_object_id = OBJECT_ID(N'[dbo].[EntityAttribute]'))
ALTER TABLE [dbo].[EntityAttribute]  WITH CHECK ADD  CONSTRAINT [FK_EntityAttribute_Entity] FOREIGN KEY([EntityID])
REFERENCES [dbo].[Entity] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EntityAttribute_EntityAttributeType]') AND parent_object_id = OBJECT_ID(N'[dbo].[EntityAttribute]'))
ALTER TABLE [dbo].[EntityAttribute]  WITH CHECK ADD  CONSTRAINT [FK_EntityAttribute_EntityAttributeType] FOREIGN KEY([EntityAttributeTypeID])
REFERENCES [dbo].[EntityAttributeType] ([ID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EntityAttributeType_EntityType]') AND parent_object_id = OBJECT_ID(N'[dbo].[EntityAttributeType]'))
ALTER TABLE [dbo].[EntityAttributeType]  WITH CHECK ADD  CONSTRAINT [FK_EntityAttributeType_EntityType] FOREIGN KEY([EntityTypeID])
REFERENCES [dbo].[EntityType] ([ID])